<template>
  <div class="login-page">
    <div class="login-top">数据服务平台</div>
    <div class="from-login-body">
      <div class="login-from-box">
        <div class="logo-img-login"></div>
        <div class="from-box">
          
          <div class="from-title">用户登录</div>
          <el-form :model="loginFrom" :rules="loginRules" ref="loginFrom" label-width="0px" class="demo-login-From" @submit.native.prevent>
            <el-form-item prop="username">
              <el-input v-model="loginFrom.username" placeholder="请输入用户名">
                <i slot="prefix" class="login-user-icon"></i>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" placeholder="请输入密码" :show-password="true" v-model="loginFrom.password">
                <i slot="prefix" class="login-pass-icon"></i>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn" native-type="submit" @click="submitLogin">
                <div class="login-text">登录</div>
              </el-button>
              <span class="visitor-login-btn" @click="VisitorLogin">游客登录</span>
              <!-- <el-button type="primary" class="login-button" native-type="submit" @click="submitLogin">登录</el-button> -->
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginFrom: {
        username: '',
        password: ''
      },
      loginRules: {
        username:[ { required: true, message: '请输入登录账户', trigger: 'blur' }, ],
        password: [ { required: true, message: '请输入登录密码', trigger: 'blur' } ]
      }
    }
  },
  methods: {
    // 游客登录
    VisitorLogin() {
      this.$store.dispatch('user/VisitorLogin').then(()=>{
        this.$router.push({
          path: '/web'
        })
      })
    },
    submitLogin() {
      const vm = this;
      vm.$refs['loginFrom'].validate((valid)=>{
        if(valid) {
          vm.$store.dispatch('user/accountLogin',vm.loginFrom).then((res)=>{
            if (res.code === '20000') {
              vm.$message.success('登录成功');
              vm.$router.push({
                path: '/web'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../styles/login.scss";
</style>